<?php

namespace Game\Core;

use Game\Helper\Log;

use Game\Helper\Statics;

class MatchCalc
{
    /**
     * Minuto quea partida ira rolar
     */
    public $minute;

    /**
     * 
     */
    public function calc(&$match, $minute)
    {
        $this->minute = $minute;
        
        # === Verifica se vai ter passe curto
        if($this->_generateProbability() <= 50) {
            $this->calcPasseCurto($nMatch);
        }
        else {
            # === Verifica se vai ter passe Longo
            if($this->_generateProbability() <= 50) {
                $this->calcPasseLongo($nMatch);
            }
            # === Verifica se vai ter cruzamento
            else {
                $this->calcCruzamento($nMatch);
            }
        }

        return $match;
    }

    /**
     * Calcula se ouve falta
     */
    protected function calcFalta(&$match)
    {
        $this->_createStatics($match, StaticsAdmin::FALTA);
        $this->_createStaticsAdmin($match, StaticsAdmin::FALTA, true);
        return $match;
    }

    /**
     * Calcula se passe curto deu certo.
     */
    protected function calcPasseCurto(&$match)
    {
        if($this->_generateProbability() <= 80) {
            $this->_createStaticsAdmin($match, StaticsAdmin::PASSE_CURTO, true);
            if($this->_generateProbability() >= 0  && $this->_generateProbability() <= 50)
            {
                $this->calcPasseCurto($match);
            }
            else if($this->_generateProbability() > 50  && $this->_generateProbability() <= 70)
            {
                $this->calcPasseLongo($match);
            }
        }
        else {
            $this->_createStaticsAdmin($match, StaticsAdmin::PASSE_CURTO, false);
        }
    }

    /**
     * Calcula se passe longo deu certo.
     */
    protected function calcPasseLongo(&$match)
    {
        if($this->_generateProbability() <= 60) {
            $this->_createStaticsAdmin($match, StaticsAdmin::PASSE_LONGO, true);
            if($this->_generateProbability() >= 0  && $this->_generateProbability() <= 40)
            {
                $this->calcPasseCurto($match);
            }
            else if($this->_generateProbability() > 50  && $this->_generateProbability() <= 70)
            {
                $this->calcPasseLongo($match);
            }
        }
        else {
            $this->_createStaticsAdmin($match, StaticsAdmin::PASSE_LONGO, false);
        }
    }

    /**
     * Calcula se cruzamento deu certo.
     */
    protected function calcCruzamento(&$match)
    {
        if($this->_generateProbability() <= 50) {
            $this->_createStaticsAdmin($match, StaticsAdmin::CRUZAMENTO, true);
            if($this->_generateProbability() > 0  && $this->_generateProbability() <= 30)
            {
                $this->calcGol($match);
            }
            else if($this->_generateProbability() > 30  && $this->_generateProbability() <= 50)
            {
                $this->calcFalta($match);
            }
        }
        else {
            $this->_createStaticsAdmin($match, StaticsAdmin::CRUZAMENTO, false);
        }
    }

    /**
     * Calcula gol de jogador/Defesa do gol
     */
    protected function calcGol(&$match)
    {
        $this->_createStatics($match, StaticsAdmin::GOL);
        $this->_createStaticsAdmin($match, StaticsAdmin::GOL, true);
    }

    /**
     * Calcula Cartão amarelo
     */
    protected function calcCartaoAmarelo(&$match)
    {
        $this->_createStaticsAdmin($match, StaticsAdmin::CARTAO_AMARELO, true);
    }

    /**
     * Calcula Cartão vermelho
     */
    protected function calcCartaoVermelho(&$match)
    {
        $this->_createStaticsAdmin($match, StaticsAdmin::CARTAO_VERMELHO, true);
    }

    /**
     * Calcula Contusão de jogador
     */
    protected function calcContusao(&$match)
    {
        $this->_createStaticsAdmin($match, StaticsAdmin::CONTUSAO, true);
    }

    /**
     * Calcula Expusao de jogador
     */
    protected function calcExpusao(&$match)
    {
        $this->_createStaticsAdmin($match, StaticsAdmin::EXPUSAO, true);
    }

    /**
     * Calcula defesa da bola.
     */
    protected function calcDefesa(&$match)
    {
        $this->_createStaticsAdmin($match, StaticsAdmin::DEFESA, true);
    }

    /**
     * Calcula o desarme (Roubar a bola)
     */
    protected function calcDesarme(&$match)
    {
        $this->_createStaticsAdmin($match, StaticsAdmin::DESARME, true);
    }

    /**
     * Gera probabilidade nos calculos
     * @return Int
     */
    private function _generateProbability()
    {
        return rand(1,100);
    }

    /**
     * Array de interação array('player' => Token do plaey, 'team' => token do time,
     * 'type' => Constante de estatistica, 'minute' => Minuto da partida, 'date' => Data e hora do servidor que foi feito a ação.)
     * @return Int
     */
    private function _createStatics(&$match, $type)
    {
        $interacion = array(
            'player' => 'Player token',
            'team' => 'Team token',
            'type'  => $type,
            'minute' => $this->minute,
            'date' => date('Y-m-d H:i:s')
        );

        array_push($match['interaction'], $interacion);

        Log::createLog("Atualizado dados da partida: ".$match['match']);
    }

    /**
     * Array para Admin para calcualar status do jogados 
     */
    private function _createStaticsAdmin(&$match, $type, $status)
    {
        $staticsAdmin = array(
            'player_inicial' => 'Token do jogador que começou',
            'player_final' => 'Token do jogador que termino',
            'status' => $status,
            'type' => $type,
            'minute' => $this->minute,
            'date' => date('Y-m-d H:i:s')
        );

        if(!isset($match['interaction_admin'])) {
            $match['interaction_admin'] = array();
        }

        array_push($match['interaction_admin'], $staticsAdmin);
    }
}
